package com.itheima.bigevent.service.impl;

import cn.hutool.json.JSONUtil;
import com.itheima.bigevent.mapper.CategoryMapper;
import com.itheima.bigevent.pojo.Category;
import com.itheima.bigevent.service.CategoryService;
import com.itheima.bigevent.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.itheima.bigevent.service.impl
 * Description:分类的实现
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-08-20-16:02
 * @Version: v1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Resource
    private StringRedisTemplate rds;
    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public Integer add(Category category) {
        Date date = new Date(System.currentTimeMillis());
        category.setCreateTime(date);
        category.setUpdateTime(date);
        // 获得全局的线程安全变量
        Map<String, Object> map = ThreadLocalUtil.get();
        category.setCreateUser((Integer) map.get("id"));

        return categoryMapper.insertSelective(category);
    }

    /**
     * 查询当前用用户创建的分类
     * @return
     */
    @Override
    public List<Category> queryList() {
        // 查询当前用户的id
        Map<String, Object> map =  ThreadLocalUtil.get();
        return categoryMapper.selectByUserId((Integer) map.get("id"));
    }

    /**
     * 查询所有的分类
     * @return
     */
    @Override
    public List<Category> queryListAll() {
        // 使用redis缓存分类信息
        // 1. 查询redis中是否有分类信息
        String categoryList = rds.opsForValue().get("category");
        if(categoryList != null && !StringUtils.isEmpty(categoryList)){
            // 2. 如果有,则直接返回
            return JSONUtil.toList(categoryList, Category.class);
        }
        // 如果没有就查询数据库
        List<Category> res =  categoryMapper.selectAll();
        // 将结果存入redis
        rds.opsForValue().set("category", JSONUtil.toJsonStr(res));
        return res;
    }

    /**
     * 根据文章的id查询文章的分类
     * @param id
     * @return
     */
    @Override
    public Category queryById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @Override
    public Integer update(Category category) {
        deleteCache();
        return categoryMapper.updateByPrimaryKey(category);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public Integer deleteById(Integer id) {
        deleteCache();
        return categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 当分类情况发生变化时, 需要删除redis中的缓存, 保证数据的一致性
     */
    public void deleteCache(){
        // 删除key
        rds.delete("category");
    }

}
